/**
 *      Created by YIn on 2017/12/6.
 *      WebSocket 封装;
 *      调用 Webs(url) 返回一个ws;
 *      url为 'ws://' + window.location.host + 端口;
 *      onmessage (evt)
 *      onopen onclose onerror
 * */

const yin_webs = (interface) => {
        // 判断是否支持ws
        if (!window.WebSocket) {
            alert('该浏览器不支持WebSocket协议,请更换Chrome浏览器访问！');
            return false;
        }
        let ws = new WebSocket('ws://' + window.location.host + interface);
        ws.last_health_time = -1; // 上一次心跳时间
        ws.keepalive = function () {
            let time = new Date().getTime();  // 当前时间
            if (ws.last_health_time !== -1 && time - ws.last_health_time > 5000) { // 判断是否断网5s？
                console.log('已断开！');
                ws.onclose();
            } else {
                // 此时last_health_time = -1 是断网后
                // 如果断网了，ws.send会无法发送消息出去
                if (ws.bufferedAmount === 0 && ws.readyState === WebSocket.OPEN) {
                    // ws.send('1');
                    ws.last_health_time = time;
                }
            }
            console.log(ws);
        };
        if (ws) {
            let reconnect = 0; //重连的时间
            let reconnectMark = false; //是否重连过
            ws.onopen = () => {
                console.log('ws: '+ interface + '连接成功！');
                reconnect = 0;
                reconnectMark = false;
                if (ws.readyState === 1) { // 为1表示连接处于open状态
                    ws.keepAliveTimer = setInterval(() => {
                        ws.keepalive();
                    }, 1000)
                }
            };
            ws.onerror = (evt) => {
                console.error('error: ' + interface + 'error evt:' + evt)
            };
            ws.onmessage = (msg) => {
                // 收到消息，重置定时器
                clearTimeout(ws.receiveMessageTimer);
                ws.receiveMessageTimer = setTimeout(() => {
                    // console.log('触发onclose');
                    ws.onclose();
                }, 30 * 1000); // 30s没收到信息，代表服务器出问题了，关闭连接。
            };
            ws.onclose = () => {
                console.log('ws: '+ interface + '断开！');
                clearTimeout(ws.receiveMessageTimer);
                clearInterval(ws.keepAliveTimer);
                if (!reconnectMark) { // 如果没有重连过，进行重连。
                    reconnect = new Date().getTime();
                    reconnectMark = true;
                }
                let tempWs = ws; // 保存ws对象
                if (new Date().getTime() - reconnect >= 30000) { // 30秒重连，连不上就不连了
                    // console.log('触发onclose');
                    ws.onclose;
                    return false;
                } else {
                    ws = new WebSocket('ws://' + window.location.host + interface);
                    ws.onopen = tempWs.onopen;
                    ws.onmessage = tempWs.onmessage;
                    ws.onerror = tempWs.onerror;
                    ws.onclose = tempWs.onclose;
                    ws.keepalive = tempWs.keepalive;
                    ws.last_health_time = -1;
                }
            }
        }
        return ws;
    };


function WebS(url) {
    // 判断是否支持ws
    if (!window.WebSocket) {
        alert('该浏览器不支持WebSocket协议,请更换Chrome浏览器访问！');
        return false;
    }
    return new WebSocket('ws://' + window.location.host + url);
}